﻿////<TunynetCopyright>
////--------------------------------------------------------------
////<copyright>青岛拓宇网络科技有限公司 2005-2008</copyright>
////<version>V0.5</verion>
////<createdate>4/4/2008 2:05:13 PM</createdate>
////<author>xier</author>
////<email>xier@tunynet.com</email>
////<log date="4/4/2008 2:05:13 PM">创建</log>
////--------------------------------------------------------------
////</TunynetCopyright>

//using System;
//using System.Collections.Generic;
//using System.Text;
//using ChinaCustoms.Qingdao.Workflow.Permissions;
//using System.Collections.ObjectModel;
//using ChinaCustoms.Qingdao.Workflow.WcfServices.ClientProxy;

//namespace ChinaCustoms.Qingdao.Workflow.Supports
//{

//    public class OrganizationRoleUserHelper
//    {
//        /// <summary>
//        /// 不用使用permissionClientDoNotUseDirectly字段,请使用PermissionClient属性
//        /// </summary>
//        PermissionClient permissionClientDoNotUseDirectly;
//        PermissionClient PermissionClient
//        {
//            get
//            {
//                if (this.permissionClientDoNotUseDirectly == null)
//                {
//                    this.permissionClientDoNotUseDirectly = new PermissionClient("iisHostEndpoint");
//                }
//                return this.permissionClientDoNotUseDirectly;
//            }
//        }

//        private Dictionary<string, GeneralOrganization> organizationCollectionAll = null;
//        /// <summary>
//        /// 所有的部门
//        /// </summary>
//        public Dictionary<string, GeneralOrganization> OrganizationCollectionAll
//        {
//            get { return this.organizationCollectionAll; }
//            set { this.organizationCollectionAll = value; }
//        }

//        private Dictionary<string, string> organizationRelationship = new Dictionary<string, string>();
//        /// <summary>
//        /// 部门间的关系
//        /// </summary>
//        /// <remarks>第一个string表示部门的ID,第二个string表示父部门的ID</remarks>
//        public Dictionary<string, string> OrganizationRelationship
//        {
//            get { return this.organizationRelationship; }
//            set { this.organizationRelationship = value; }
//        }

//        public Dictionary<string, string> organizationCollectionSearchedUndone = new Dictionary<string, string>();
//        /// <summary>
//        /// 需要搜索的部门集合,即需要显示在树上的部门(未处理的，即还没有排定组织的结构关系)
//        /// </summary>
//        /// <remarks>第一个string表示部门的ID,第二个bool(true显示下面的全部用户,flase显示角色中指定的用户)</remarks>
//        public Dictionary<string, string> OrganizationCollectionSearchedUndone
//        {
//            get { return this.organizationCollectionSearchedUndone; }
//            set { this.organizationCollectionSearchedUndone = value; }
//        }

//        public Dictionary<string, string> organizationCollectionSearchedDone = new Dictionary<string, string>();
//        /// <summary>
//        /// 需要搜索的部门集合,即需要显示在树上的部门(以处理的，即已经排定组织的结构关系)
//        /// </summary>
//        /// <remarks>第一个string表示部门的ID,第二个bool(true显示下面的全部用户,flase显示角色中指定的用户)</remarks>
//        public Dictionary<string, string> OrganizationCollectionSearchedDone
//        {
//            get { return this.organizationCollectionSearchedDone; }
//            set { this.organizationCollectionSearchedDone = value; }
//        }

//        private Dictionary<string, GeneralOGUObject> usersNeedDisplay = new Dictionary<string, GeneralOGUObject>();
//        /// <summary>
//        /// 需要显示在树上的人员
//        /// </summary>
//        public Dictionary<string, GeneralOGUObject> UsersNeedDisplay
//        {
//            get { return this.usersNeedDisplay; }
//            set { this.usersNeedDisplay = value; }
//        }

//        /// <summary>
//        /// 构造函数
//        /// </summary>
//        public OrganizationRoleUserHelper():this(null)
//        {
//        }

//        /// <summary>
//        /// 构造函数
//        /// </summary>
//        public OrganizationRoleUserHelper(GeneralOrganization rootOrg)
//        {
//            this.GetOrganizationCollectionAll(rootOrg);
//            this.GetOrganizationRelationship();
//        }

//        #region 基础方法
//        /// <summary>
//        /// 获取跟节点下所有的节点(递归)
//        /// </summary>
//        /// <param name="rootOrg"></param>
//        public Dictionary<string, GeneralOrganization> GetOrganizationCollectionAll(GeneralOrganization rootOrg)
//        {
//            if (rootOrg == null)
//            {
//                rootOrg = this.PermissionClient.GetRootOrganization();
//            }

//            organizationCollectionAll = this.PermissionClient.GetOrganizations(rootOrg.ID);
//            return this.organizationCollectionAll;
//        }

//        /// <summary>
//        /// 设置部门间的关系
//        /// </summary>
//        /// <remarks>即是让部门ID跟其父ID对应</remarks>
//        public Dictionary<string, string> GetOrganizationRelationship()
//        {
//            if (this.OrganizationCollectionAll != null)
//            {
//                int orgCount = this.OrganizationCollectionAll.Count;

//                foreach (KeyValuePair<string, GeneralOrganization> pair in this.OrganizationCollectionAll)
//                {
//                    this.OrganizationRelationship.Add(pair.Key, pair.Value.ParentID);
//                }
//            }

//            return this.OrganizationRelationship;
//        }

//        /// <summary>
//        /// 向未处理部门列表添加新的未处理部门
//        /// </summary>
//        /// <param name="orgIDs"></param>
//        /// <returns></returns>
//        public void AddOrganizationCollectionSearchedUndone(string[] orgIDs)
//        {
//            if (orgIDs != null)
//            {
//                for (int i = 0; i < orgIDs.Length; i++)
//                {
//                    if (OrganizationCollectionSearchedUndone.ContainsKey(orgIDs[i]) == false && this.organizationRelationship.ContainsKey(orgIDs[i]) == true)
//                    {
//                        OrganizationCollectionSearchedUndone.Add(orgIDs[i], this.organizationRelationship[orgIDs[i]]);
//                    }
//                }
//            }
//        }

//        /// <summary>
//        /// 把所有的部门加入到未处理部门列表中
//        /// </summary>
//        public void AddOrganizationCollectionSearchedUndone()
//        {
//            foreach (KeyValuePair<string, GeneralOrganization> kvp in this.OrganizationCollectionAll)
//            {
//                if (this.organizationCollectionSearchedUndone.ContainsKey(kvp.Key) == false)
//                {
//                    this.organizationCollectionSearchedUndone.Add(kvp.Key, kvp.Value.ParentID);
//                }
//            }
//        }
//        #endregion


//        #region 通过未整理的部门集合，获取整理后的部门集合
//        /// <summary>
//        /// 通过未整理的部门集合，获取整理后的部门集合
//        /// </summary>
//        /// <param name="organizationCollectionSearchedUndone">未整理的部门集合</param>
//        /// <remarks>未整理的部门是许多没有关系的凌乱的部门，整理后的集合中 包括未整理部门的 父部门(一直递归到树根)和所有子部门(递归到叶子)</remarks>
//        public Dictionary<string, string> GetOrganizationCollectionSearchedDone(Dictionary<string, string> organizationCollectionSearchedUndone)
//        {
//            foreach (KeyValuePair<string, string> kvp in organizationCollectionSearchedUndone)
//            {
//                AddSearchedOrganizationParent(kvp.Key);
//                AddSearchedOrganizationChildren(kvp.Key);
//            }

//            return organizationCollectionSearchedDone;
//        }

//        /// <summary>
//        /// 添加搜索部门的所有父部门，一直递归到根节点
//        /// </summary>
//        /// <param name="orgID"></param>
//        private void AddSearchedOrganizationParent(string orgID)
//        {
//            if (this.OrganizationRelationship.ContainsKey(orgID))
//            {
//                if (this.organizationCollectionSearchedDone.ContainsKey(orgID) == false)
//                {
//                    this.organizationCollectionSearchedDone.Add(orgID, this.OrganizationRelationship[orgID]);
//                }

//                string tempPareentID = this.OrganizationRelationship[orgID];
//                if (string.IsNullOrEmpty(tempPareentID) == false)
//                {
//                    AddSearchedOrganizationParent(tempPareentID);
//                }
//            }
//        }

//        /// <summary>
//        /// 添加搜索部门的所有子部门，一直递归到叶子节点
//        /// </summary>
//        /// <param name="orgID"></param>
//        private void AddSearchedOrganizationChildren(string orgID)
//        {
//            if (this.OrganizationRelationship.ContainsKey(orgID))
//            {
//                if (this.organizationCollectionSearchedDone.ContainsKey(orgID) == false)
//                {
//                    this.organizationCollectionSearchedDone.Add(orgID, this.OrganizationRelationship[orgID]);
//                }

//                foreach (KeyValuePair<string, string> kvp in this.OrganizationRelationship)
//                {
//                    if (kvp.Value == orgID)
//                    {
//                        AddSearchedOrganizationChildren(kvp.Key);
//                    }
//                }
//            }
//        }
//        #endregion


//        #region 获取角色内的所有对象
//        /// <summary>
//        /// 获取角色内的所有对象
//        /// </summary>
//        public void GetObjectsInRoles(string applicationCodeName, string[] roleNameArray)
//        {
//            ReadOnlyCollection<GeneralOGUObject> oguColl = this.PermissionClient.GetObjectsInRoles(applicationCodeName, roleNameArray);

//            string orgIDInRoles = string.Empty;
//            for (int i = 0; i < oguColl.Count; i++)
//            {
//                GeneralOGUObject tempOgu = oguColl[i];
//                switch (tempOgu.SchemaType)
//                {
//                    case ObjectSchemaType.Groups:
//                        //用户组需要进一步处理(需要向人员显示集合里面写数据)
//                        break;
//                    case ObjectSchemaType.Organizations:
//                    case ObjectSchemaType.OrganizationsInRole:
//                        orgIDInRoles += tempOgu.ID + ",";
//                        this.DealWithOrgInRole1(tempOgu.ID);
//                        break;
//                    case ObjectSchemaType.User:
//                        if (this.usersNeedDisplay.ContainsKey(tempOgu.ID) == false)
//                        {
//                            this.usersNeedDisplay.Add(tempOgu.ID, tempOgu);
//                        }
//                        break;
//                    default:
//                        break;
//                }
//            }

//            if (orgIDInRoles != string.Empty)
//            {
//                if (orgIDInRoles.EndsWith(",") == true)
//                {
//                    orgIDInRoles = orgIDInRoles.Substring(0, orgIDInRoles.Length - 1);
//                }
//                string[] orgIDs = orgIDInRoles.Split(',');
//                this.DealWithOrgInRole2(orgIDs);
//            }
//        }

//        /// <summary>
//        /// 处理角色内的部门,将其加入 部门未处理列表
//        /// </summary>
//        private void DealWithOrgInRole1(string orgID)
//        {
//            if (this.organizationCollectionSearchedUndone.ContainsKey(orgID) == false && this.organizationRelationship.ContainsKey(orgID))
//            {
//                this.organizationCollectionSearchedUndone.Add(orgID, this.organizationRelationship[orgID]);
//            }
//        }

//        /// <summary>
//        /// 处理角色内的部门(把部门下的所有用户加入 显示集合)
//        /// </summary>
//        private void DealWithOrgInRole2(string[] orgIDs)
//        {
//            ReadOnlyCollection<IUser> userColl = this.PermissionClient.GetUsersInOrganizations(orgIDs);
//            if (userColl != null)
//            {
//                for (int i = 0; i < userColl.Count; i++)
//                {
//                    if (this.usersNeedDisplay.ContainsKey(userColl[i].ID) == false)
//                    {
//                        this.usersNeedDisplay.Add(userColl[i].ID, userColl[i]);
//                    }
//                }
//            }
//        }

//        ///// <summary>
//        ///// 获取角色内的所有对象
//        ///// </summary>
//        //public void GetObjectsInRoles(string applicationCodeName, string[] roleNameArray, out Dictionary<string, GeneralOGUObject> usersInRoles, out Dictionary<string, string> orgsInRoles)
//        //{
//        //    usersInRoles= new Dictionary<string, GeneralOGUObject>();
//        //    orgsInRoles = new Dictionary<string, string>();

//        //    ReadOnlyCollection<GeneralOGUObject> oguColl = this.PermissionClient.GetObjectsInRoles(applicationCodeName, roleNameArray);

//        //    string orgIDInRoles = string.Empty;
//        //    for (int i = 0; i < oguColl.Count; i++)
//        //    {
//        //        GeneralOGUObject tempOgu = oguColl[i];
//        //        switch (tempOgu.SchemaType)
//        //        {
//        //            case ObjectSchemaType.Groups:
//        //                //用户组需要进一步处理(需要向人员显示集合里面写数据)
//        //                break;
//        //            case ObjectSchemaType.Organizations:
//        //            case ObjectSchemaType.OrganizationsInRole:
//        //                orgIDInRoles += tempOgu.ID + ",";
//        //                this.DealWithOrgInRole1(tempOgu.ID, orgsInRoles);
//        //                break;
//        //            case ObjectSchemaType.User:
//        //                if (usersInRoles.ContainsKey(tempOgu.ID) == false)
//        //                {
//        //                    usersInRoles.Add(tempOgu.ID, tempOgu);
//        //                }
//        //                break;
//        //            default:
//        //                break;
//        //        }
//        //    }

//        //    if (orgIDInRoles != string.Empty)
//        //    {
//        //        if (orgIDInRoles.EndsWith(",") == true)
//        //        {
//        //            orgIDInRoles = orgIDInRoles.Substring(0, orgIDInRoles.Length - 1);
//        //        }
//        //        string[] orgIDs = orgIDInRoles.Split(',');
//        //        this.DealWithOrgInRole2(orgIDs, usersInRoles);
//        //    }
//        //}

//        ///// <summary>
//        ///// 处理角色内的部门,将其加入 部门未处理列表
//        ///// </summary>
//        //private void DealWithOrgInRole1(string orgID, Dictionary<string, string> orgsInRoles)
//        //{
//        //    if (orgsInRoles.ContainsKey(orgID) == false && this.organizationRelationship.ContainsKey(orgID))
//        //    {
//        //        orgsInRoles.Add(orgID, this.organizationRelationship[orgID]);
//        //    }
//        //}

//        ///// <summary>
//        ///// 处理角色内的部门(把部门下的所有用户加入 显示集合)
//        ///// </summary>
//        //private void DealWithOrgInRole2(string[] orgIDs, Dictionary<string, GeneralOGUObject> usersInRoles)
//        //{
//        //    ReadOnlyCollection<IUser> userColl = this.PermissionClient.GetUsersInOrganizations(orgIDs);
//        //    if (userColl != null)
//        //    {
//        //        for (int i = 0; i < userColl.Count; i++)
//        //        {
//        //            if (usersInRoles.ContainsKey(userColl[i].ID) == false)
//        //            {
//        //                usersInRoles.Add(userColl[i].ID, userColl[i]);
//        //            }
//        //        }
//        //    }
//        //}
//        #endregion
//    }
//}
